<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
    <head>
        <th:block th:include="include :: header('新增用户')"/>
        <th:block th:include="include :: select2-css"/>
    </head>
    <body>
        <div class="main-content">
            <form class="form-horizontal" id="form-user-add">
                <input id="treeId" name="deptId" type="hidden"/>
                <h4 class="form-header h4">基本信息</h4>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label is-required">用户名称：</label>
                            <div class="col-sm-8">
                                <input class="form-control" maxlength="30" name="userName" placeholder="请输入用户名称"
                                       required type="text">
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">归属部门：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input class="form-control" id="treeName" name="deptName" onclick="selectDeptTree()"
                                           placeholder="请选择归属部门" type="text">
                                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">手机号码：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input class="form-control" id="phonenumber" maxlength="11"
                                           name="phonenumber" placeholder="请输入手机号码" type="text">
                                    <span class="input-group-addon"><i class="fa fa-mobile"></i></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">邮箱：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input class="form-control email" id="email" maxlength="50" name="email"
                                           placeholder="请输入邮箱"
                                           type="text">
                                    <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label is-required">登录账号：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="loginName" maxlength="30" name="loginName"
                                       placeholder="请输入登录账号" required type="text">
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label is-required">登录密码：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input class="form-control" id="password" name="password"
                                           placeholder="请输入登录密码" required
                                           th:value="${@config.getKey('sys.user.initPassword')}" type="password">
                                    <span class="input-group-addon" onmousedown="$('#password').attr('type','text')"
                                          onmouseup="$('#password').attr('type','password')"
                                          title="登录密码,鼠标按下显示密码"><i
                                            class="fa fa-key"></i></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户性别：</label>
                            <div class="col-sm-8">
                                <select class="form-control m-b" name="sex"
                                        th:with="type=${@dict.getType('sys_user_sex')}">
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                            th:value="${dict.dictValue}"></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户状态：</label>
                            <div class="col-sm-8">
                                <label class="toggle-switch switch-solid">
                                    <input checked id="status" type="checkbox">
                                    <span></span>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <div class="form-group">
                            <label class="col-xs-2 control-label">岗位：</label>
                            <div class="col-xs-4">
                                <select class="form-control select2-multiple" id="post" multiple>
                                    <option th:disabled="${post.status == '1'}" th:each="post:${posts}"
                                            th:text="${post.postName}"
                                            th:value="${post.postId}"></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <div class="form-group">
                            <label class="col-xs-2 control-label">角色：</label>
                            <div class="col-xs-10">
                                <label class="check-box" th:each="role:${roles}">
                                    <input name="role" th:disabled="${role.status == '1'}" th:text="${role.roleName}"
                                           th:value="${role.roleId}" type="checkbox">
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <h4 class="form-header h4">其他信息</h4>
                <div class="row">
                    <div class="col-sm-12">
                        <div class="form-group">
                            <label class="col-xs-2 control-label">备注：</label>
                            <div class="col-xs-10">
                                <textarea class="form-control" maxlength="500" name="remark" rows="3"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="row">
            <div class="col-sm-offset-5 col-sm-10">
                <button class="btn btn-sm btn-primary" onclick="submitHandler()" type="button"><i
                        class="fa fa-check"></i>保 存
                </button>&nbsp;
                <button class="btn btn-sm btn-danger" onclick="closeItem()" type="button"><i
                        class="fa fa-reply-all"></i>关 闭
                </button>
            </div>
        </div>
        <th:block th:include="include :: footer"/>
        <th:block th:include="include :: select2-js"/>
        <script>
            var prefix = ctx + "system/user";

            $("#form-user-add").validate({
                onkeyup: false,
                rules: {
                    loginName: {
                        minlength: 2,
                        maxlength: 20,
                        remote: {
                            url: prefix + "/checkLoginNameUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "loginName": function () {
                                    return $.common.trim($("#loginName").val());
                                }
                            }
                        }
                    },
                    password: {
                        minlength: 5,
                        maxlength: 20,
                        specialSign: true
                    },
                    email: {
                        email: true,
                        remote: {
                            url: prefix + "/checkEmailUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "email": function () {
                                    return $.common.trim($("#email").val());
                                }
                            }
                        }
                    },
                    phonenumber: {
                        isPhone: true,
                        remote: {
                            url: prefix + "/checkPhoneUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "phonenumber": function () {
                                    return $.common.trim($("#phonenumber").val());
                                }
                            }
                        }
                    },
                },
                messages: {
                    "loginName": {
                        remote: "用户已经存在"
                    },
                    "email": {
                        remote: "Email已经存在"
                    },
                    "phonenumber": {
                        remote: "手机号码已经存在"
                    }
                },
                focusCleanup: true
            });

            function submitHandler() {
                var chrtype = [[${#strings.defaultString(@config.getKey('sys.account.chrtype'), 0)}]];
                var password = $("#password").val();
                if ($.validate.form() && checkpwd(chrtype, password)) {
                    var data = $("#form-user-add").serializeArray();
                    var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                    var roleIds = $.form.selectCheckeds("role");
                    var postIds = $.form.selectSelects("post");
                    data.push({"name": "status", "value": status});
                    data.push({"name": "roleIds", "value": roleIds});
                    data.push({"name": "postIds", "value": postIds});
                    $.operate.saveTab(prefix + "/add", data);
                }
            }

            /* 用户管理-新增-选择部门树 */
            function selectDeptTree() {
                var treeId = $("#treeId").val();
                var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
                var url = ctx + "system/user/selectDeptTree/" + deptId;
                var options = {
                    title: '选择部门',
                    width: "380",
                    url: url,
                    callBack: doSubmit
                };
                $.modal.openOptions(options);
            }

            function doSubmit(index, layero) {
                var body = $.modal.getChildFrame(index);
                $("#treeId").val(body.find('#treeId').val());
                $("#treeName").val(body.find('#treeName').val());
                $.modal.close(index);
            }

            $(function () {
                $('#post').select2({
                    placeholder: "请选择岗位",
                    allowClear: true
                });
            })
        </script>
    </body>
</html>